import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login'
import Home from './components/Home'
import Welcome from "@/components/Welcome";
import Users from "@/components/user/Users";
import Rights from "@/components/power/Rights";
import Roles from "@/components/power/Roles";
import Cate from "@/components/goods/Cate";
import Params from "@/components/goods/Params";
import List from "@/components/goods/List";
import Add from "@/components/goods/Add";
import Order from "@/components/order/Order";
import Report from "@/components/report/Report";

Vue.use(Router);

const router = new Router({
    routes:[
        {path:'/login',component:Login},
        {path: '/',redirect:'/login'},
        {
            path:'/home',
            component:Home,
            redirect:'/home/welcome',
            children:[
                {path:'/home/welcome', component: Welcome},
                {path:'/home/users',component: Users},
                {path:'/home/rights',component:Rights},
                {path:'/home/roles',component:Roles},
                {path: '/home/categories',component:Cate},
                {path: '/home/params',component:Params},
                {path: '/home/goods',component:List},
                {path:'/home/add',component:Add},
                {path:'/home/orders',component:Order},
                {path:'/home/reports',component:Report},
            ]
        }
    ]
});

// 挂载路由导航守卫
router.beforeEach((to, from, next)=>{
    // to 将要访问的路径
    // from 从那个路径跳转
    // next 是一个函数，表示放行去哪里
    if(to.path==='/login') return next();

    // 获取 token
    const token = window.sessionStorage.getItem('token');
    if(token){
        next();
    }else{
        next('/login');
    }
});

export default router;